package com.kloyf.config.datasource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 数据源AOP
 **/
@Aspect
@Component
@Order(0)
public class DataSourceAspect {
    private Logger logger = LoggerFactory.getLogger(DataSourceAspect.class);


    /**
     * 前置切片
     * 打印输出数据源
     *
     * @param point
     */
    @Before("execution(* com.kloyf.*.*(..))")
    public void before(JoinPoint point) {
        logger.debug("======>  AOP before start  ");
        final Object target = point.getTarget();
        final String method = point.getSignature().getName();

        final Class<?>[] classz = target.getClass().getInterfaces();

        final Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
        try {
            final Method m = classz[0].getMethod(method, parameterTypes);
            if (m != null && m.isAnnotationPresent(DataSource.class)) {
                final DataSource data = m.getAnnotation(DataSource.class);
                DataSourceContextHolder.putDataSource(data.value());
                logger.debug("======>        用户选择数据库源：" + DataSourceContextHolder.getDataSouce());
            }
        } catch (final Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        logger.debug("======>  AOP before end  ");
    }
/**
 * 已经废弃
 * 但是代码不要删除。
 */
//    public void before(JoinPoint point) {
//        logger.debug("======>  AOP before start  ");
//        Object target = point.getTarget();
//        logger.debug(target.toString());
//        String method = point.getSignature().getName();
//        Class<?>[] classz = target.getClass().getInterfaces();
//        Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
//        try {
//            Method m = classz[0].getMethod(method, parameterTypes);
//            if (m != null) {
//                logger.debug("======>        用户选择数据库源：" + DataSourceContextHolder.getDataSouce());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        logger.debug("======>  AOP before end  ");
//    }

//    /**
//     * 后置切片
//     * 将数据源切换为主数据源
//     * 废弃
//     * @param point
//     */
//    @After("execution(* com.guoguo.api.service.*.*(..))")
//    public void after(JoinPoint point) {
//        logger.debug("======>  AOP After start  ");
//
//        ResourceBundle rb = ResourceBundle.getBundle("jdbc", Locale.getDefault());
//        String[] dataSourceNameArray = rb.getString(DataSourceEnum.DATASOURCE_NAME.getValue()).split(DataSourceEnum.COMMA.getValue());
//
//        for (String markName : dataSourceNameArray) {
//            String databaseName = markName + DataSourceEnum.POINT.getValue() + DataSourceEnum.DATABASE_NAME.getValue();
//            if (DataSourceContextHolder.getDataSouce().equals("master")) {
//                DataSourceContextHolder.putDataSource(rb.getString(databaseName));
//            }
//        }
//        logger.debug("=============="+DataSourceContextHolder.getDataSouce());
//        logger.debug("======>  AOP After start  ");
//    }
}